<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of SoftwareController
 *
 * @author thanhdx
 */
class VersionController extends Controller {

    public $layout = 'column2';
    private $__model;

    public function actionCreate() {
        $model = $this->loadModel();
        $this->performAjaxValidation($model); //validate via ajax
        if ($_POST['Version']) {
            $model->attributes = $_POST['Version'];
            if ($model->validate()) {
                $file = CUploadedFile::getInstance($model, 'file_upload');
                if (!$file->getHasError()) {
                    $model->code = md5($file->getName());
                    $model->path = 'store/' . $file->getName();
                    $model->size = $file->getSize();
                    $check_exist = Version::model()->find();
                    $count_item = $check_exist->count("code='" . $model->code . "'");
                    if (!$count_item) {
                        if ($model->save()) {
                            if (!$file->saveAs('store/' . $file->getName())) {
                                die('Not save file');
                            }
                        } else {
                            Yii::app()->user->setFlash('msg_error', 'Không lưu được!');
                        }
                    } else {
                        Yii::app()->user->setFlash('msg_error', 'Phần mềm đã tồn tại!');
                    }
                }else{
                    Yii::app()->user->setFlash('msg_error', 'Lỗi upload File!');
                }
            }
        }
        $this->render("create", array('model' => $model));
    }

    public function getSw() {
        $all_soft = Software::model()->findAll('status=1 AND cate_id=1');
        $softwares = array();
        if ($all_soft) {
            foreach ($all_soft as $sw) {
                $softwares[$sw->id] = $sw->name;
            }
        }
        return $softwares;
    }

    public function actionGetsw() {
        $cate_id = $_POST['cate'];
        $model = Software::model()->findAll('cate_id=' . $cate_id);
        $data = CHtml::listData($model, 'id', 'name');
        $sw = '';
        foreach ($data as $value => $name) {
            $sw .= CHtml::tag('option', array('value' => $value), CHtml::encode($name), true);
        }
        echo CJSON::encode(array('sw' => $sw));
    }

    public function actionUpdate($id) {

        $model = $this->loadModel($id);
        $this->performAjaxValidation($model); //validate via ajax
        if ($_POST['Version']) {
            $model->attributes = $_POST['Version'];
            if ($model->validate()) {
                $file = CUploadedFile::getInstance($model, 'file_upload');
                if (!$file->getHasError()) {
                    $model->code = md5($file->getName());
                    $model->path = 'store/' . $file->getName();
                    $model->size = $file->getSize();
                    $check_exist = Version::model()->find();
                    $count_item = $check_exist->count("code='" . $model->code . "'");
                    if (!$count_item) {
                        if ($model->save()) {
                            if (!$file->saveAs('store/' . $file->getName())) {
                                die('Not save file');
                            }
                        } else {
                            Yii::app()->user->setFlash('msg_error', 'Không lưu được!');
                        }
                    } else {
                        Yii::app()->user->setFlash('msg_error', 'Phần mềm đã tồn tại!');
                    }
                }else{
                    Yii::app()->user->setFlash('msg_error', 'Lỗi upload File!');
                }
            }
        }
        $this->render("edit", array('model' => $model));
    }

    public function actionIndex() {
        $model = new Version('search');
        $model->unsetAttributes();  // clear any default values
        if (isset($_GET['Version']))
            $model->attributes = $_GET['Version'];

        $this->render('list', array(
            'model' => $model,
        ));
    }
    
    public function actionDelete($id){
        $model = $this->loadModel($id);
        $model->delete();
    }

    private function loadModel($id=null) {
        if ($this->__model === null) {
            if ($id != null) {
                $this->__model = Version::model()->findByPk($id);
            } else {
                $this->__model = new Version;
            }
            if ($this->__model === null)
                throw new CHttpException(404, 'The requested page does not exist.');
        }
        return $this->__model;
    }

    protected function performAjaxValidation($model) {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'software-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }

}

?>
